﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace QLCV
{
    public partial class frmDonVi : Form
    {
        public frmDonVi()
        {
            InitializeComponent();
        }
        private void LoadHeThongcbn()
        {
            cbnHeThongThem.DisplayMember = "Ten";
            cbnHeThongThem.ValueMember = "ID";

            cbnHeThongSua.DisplayMember = "Ten";
            cbnHeThongSua.ValueMember = "ID";

            cbnHeThongMoi.DisplayMember = "Ten";
            cbnHeThongMoi.ValueMember = "ID";

            List<HeThongDTO> tmp = HeThongBUS.LoadAll();
            cbnHeThongThem.DataSource = tmp;
            cbnHeThongSua.DataSource = new List<HeThongDTO>(tmp);
            cbnHeThongMoi.DataSource = new List<HeThongDTO>(tmp);

        }
        private void LoadDonVilst()
        {
            lstDanhSachDonVi.DisplayMember = "Ten";
            lstDanhSachDonVi.ValueMember = "ID";
            lstDanhSachDonVi.DataSource = DonViBUS.LoadAll(((int)(cbnHeThongSua.SelectedValue)), false);
        }
        private void frmDonVi_Load(object sender, EventArgs e)
        {
            btnThem.Enabled = false;
            btnLuu.Enabled = false;
            LoadHeThongcbn();
            lstDanhSachDonVi.SelectedIndex = -1;
            cbnHeThongMoi.SelectedIndex = -1;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtDonViMoi_TextChanged(object sender, EventArgs e)
        {
            if (txtDonViMoi.Text.Trim() == "")
            {
                btnThem.Enabled = false;
            }
            else
            {
                btnThem.Enabled = true;
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            try
            {
                HeThongDTO tmpHeThong = new HeThongDTO();
                tmpHeThong = (HeThongDTO)cbnHeThongMoi.Items[cbnHeThongMoi.SelectedIndex];

                DonViDTO tmpDonVi = new DonViDTO();
                tmpDonVi = (DonViDTO)lstDanhSachDonVi.Items[lstDanhSachDonVi.SelectedIndex];
                tmpDonVi.Ten = txtDonViSua.Text.Trim();
                tmpDonVi.HeThong = tmpHeThong;

                if (DonViBUS.Edit(tmpDonVi))
                {
                    txtDonViSua.Text = "";
                    LoadDonVilst();
                    lstDanhSachDonVi.SelectedIndex = -1;
                }
                else
                {
                    MessageBox.Show("Không thể sửa thông tin đơn vị.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        private void txtDonViSua_TextChanged(object sender, EventArgs e)
        {
            if (txtDonViSua.Text.Trim() == "")
            {
                btnLuu.Enabled = false;
            }
            else
            {
                btnLuu.Enabled = true;
            }
        }

        private void cbnHeThongSua_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbnHeThongSua.SelectedIndex == -1)
            {
                lstDanhSachDonVi.SelectedIndex = -1;
                return;
            }
            LoadDonVilst();
            lstDanhSachDonVi.SelectedIndex = -1;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            try
            {
                HeThongDTO tmpHeThong = new HeThongDTO();
                tmpHeThong = (HeThongDTO)cbnHeThongThem.Items[cbnHeThongThem.SelectedIndex];

                DonViDTO tmpDonVi = new DonViDTO();
                tmpDonVi.Ten = txtDonViMoi.Text.Trim();
                tmpDonVi.HeThong = tmpHeThong;

                if (DonViBUS.Add(tmpDonVi))
                {
                    txtDonViMoi.Text = "";
                    btnThem.Enabled = false;

                    for (int i = 0; i < cbnHeThongSua.Items.Count; i++)
                    {
                        if (((HeThongDTO)(cbnHeThongSua.Items[i])).ID == tmpHeThong.ID)
                        {
                            cbnHeThongSua.SelectedIndex = i;
                            break;
                        }
                    }

                    LoadDonVilst();
                    lstDanhSachDonVi.SelectedIndex = -1;

                }
                else
                {
                    MessageBox.Show("Không thể thêm đơn vị mới.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        private void lstDanhSachDonVi_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstDanhSachDonVi.SelectedIndex == -1)
            {
                txtDonViSua.Text = "";
                txtDonViSua.Enabled = false;
                btnLuu.Enabled = false;
                cbnHeThongMoi.SelectedIndex = -1;
                cbnHeThongMoi.Enabled = false;
                return;
            }
            txtDonViSua.Text = ((DonViDTO)(lstDanhSachDonVi.SelectedItem)).Ten;
            txtDonViSua.Enabled = true;
            btnLuu.Enabled = true;
            cbnHeThongMoi.Enabled = true;
            int tmp = ((DonViDTO)(lstDanhSachDonVi.SelectedItem)).HeThong.ID;
            for (int i = 0; i < cbnHeThongMoi.Items.Count; i++)
            {
                if (((HeThongDTO)(cbnHeThongMoi.Items[i])).ID == tmp)
                {
                    cbnHeThongMoi.SelectedIndex = i;
                    break;
                }
            }
        }
    }
}
